package com.hrt.datasender.datacollect.drive.modbus.tcp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.btcode.schedule.ScheduleManager;
import com.hrt.datasender.datacollect.drive.DriveBase;
import com.hrt.datasender.datacollect.drive.DriveConfigModel;
import com.hrt.datasender.datacollect.drive.DriveParameterMetadata;
import com.hrt.datasender.datacollect.drive.modbus.rtu.ModbusSerialDataAsker;
import com.hrt.datasender.datadao.model.BcDevicesModel;
import com.hrt.datasender.datadao.model.BdSourceModel;
import com.hrt.datasender.mq.MqKit;
import com.hrt.datasender.mq.MqKit.MsgListener;
import com.hrt.datasender.mq.model.MqModel;
import com.hrt.datasender.mq.model.NetExportDataModel;

public abstract class NetBaseDrive extends DriveBase {

    private String ip;
    private String port;
    private String driveInstanceId;

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getDriveInstanceId() {
        return driveInstanceId;
    }

    public void setDriveInstanceId(String driveInstanceId) {
        this.driveInstanceId = driveInstanceId;
    }

    /**
     * 注册消息监听
     */
    protected void registerMQListener(BcDevicesModel device, HashMap<Integer, String> paramCodeMap,
            DriveConfigModel configs) {

        setIp(configs.getConfig().get("ip"));
        setPort(configs.getConfig().get("port"));
        setDriveInstanceId(configs.getConfig().get("driveInstanceId"));

        MqKit.consumeMsgFromNetExportMQ(this, new MsgListener() {
            @Override
            public void onReceive(MqModel mqModel) {
                NetExportDataModel model = (NetExportDataModel) mqModel;
                List<BdSourceModel> datas = processNetExportData(model.getData(), paramCodeMap, configs);
                setSourceData(datas);
            }
        });
    }

    /**
     * 注册数据询问定时任务，这里使用 {@link ModbusSerialDataAsker} 作为定时器 <br>
     * 定时器不能通过构造函数传参，只能通过构造HashMap<String, Object>的方式进行传参
     */
    protected void registerAskDataJob(BcDevicesModel device, DriveConfigModel configs) {
        HashMap<String, Object> data = new HashMap<>();
        data.put("device", device);
        data.put("deviceConfigs", configs);
        String jobName = getClass().getName() + "." + device.getId();
        ScheduleManager.getInstance().addJob(ModbusNetDataAsker.class, getConsStr(), data, jobName);
    }

    /**
     * 从设备中获取原始数据(网口)
     * 
     * @return
     */
    public abstract List<BdSourceModel> processNetExportData(byte[] data, HashMap<Integer, String> paramCodeMap,
            DriveConfigModel deviceConfigs);

    @Override
    public List<DriveParameterMetadata> getParameterMetadata() {
        List<DriveParameterMetadata> params = new ArrayList<>();
        params.add(new DriveParameterMetadata("ip", "IP"));
        params.add(new DriveParameterMetadata("port", "端口"));
        params.add(new DriveParameterMetadata("slaveNo", "从站编号"));
        params.add(new DriveParameterMetadata("startNo", "起始位置"));
        params.add(new DriveParameterMetadata("dataLength", "数据长度"));
        params.add(new DriveParameterMetadata("driveInstanceId", "驱动实例ID"));
        return params;
    }

}